#include "gui/buttonpanel.h"

#include "gui/button.h"
#include "gui/defaultdefines.h"
#include "gui/guimessage.h"

ButtonPanel::ButtonPanel(std::string name, int x, int y, int columns, int rows, int padding):Widget(),rows(rows),columns(columns),padding(padding){
    this->name = name;
    this->x = x;
    this->y = y;
    this->z = 0;
    SetWidth(columns*DEFAULT_BUTTON_WIDTH + (columns+1)*DEFAULT_BUTTON_PADDING);
    SetHeight(rows*DEFAULT_BUTTON_WIDTH + (rows+1)*DEFAULT_BUTTON_PADDING);
    style = PS_NO_STYLE;

    BuildVBO();
}

void ButtonPanel::BuildVBO(){
    int vertices[] = { 0, 0, 0, 1,
                       0, height, 0, 1,
                       width, height, 0, 1,
                       width, 0, 0, 1};

    float uvs[] = {0, 0,
                   0, 1,
                   1, 1,
                   1, 0,};
    numVertices = 8;
    Widget::BuildVBO(vertices, numVertices, uvs);
}

void ButtonPanel::AddButton(std::unique_ptr<Button> button){
    if(children.size() >= rows*columns){
        std::cout<<"WARNING: Available slots in button panel "<<name<<" exceeded!\n";
    }
    int btnIndex = children.size();
    int xIndex = btnIndex%columns;
    int yIndex = btnIndex/columns;

    button->Setx(x + (xIndex)*(DEFAULT_BUTTON_WIDTH+DEFAULT_BUTTON_PADDING) + DEFAULT_BUTTON_PADDING);
    button->Sety(y + GetHeight() - (yIndex+1)*(DEFAULT_BUTTON_WIDTH+DEFAULT_BUTTON_PADDING));
    button->Setz(z+5);

    AddChild(std::move(button));
}
